package com.example.exchange;

import java.rmi.RemoteException;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;

import com.example.exchange.XMLHeaderConstants;
import com.example.exchange.MapUtils;
import com.example.exchange.ServiceUtil;
import com.example.exchange.UnifierWebServicesUfbs;
import com.example.exchange.XMLObject;

/**
 * 
 * Copyright © 2019 KANGPING Info. All rights reserved.
 * 
 * Project NAME：campin.primavera.integration   
 * Class NAME：BPServiceImplV1, SOAP Webservice
 * Author：Kangping Zhou
 * Date：Aug 15, 2019 11:23:19 AM      
 * Description：kangpingchn@gmail.com
 * @version    
 *
 */
public class BPWebService {
	static Logger logger = Logger.getLogger(BPWebService.class);
	
	UnifierWebServicesUfbs service = new UnifierWebServicesUfbs();


	/**
	 * @Title: listBP 
	 * @Description: TODO
	 * @param @param clazz
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param fieldnames 
	 * @param @param filterCondition
	 * @param @param filtervalues
	 * @param @return 
	 * @return List<Object>
	 * @throws
	 */
	public List<Object> listBP(Class clazz, String projectnumber, String bpname, String[] fieldnames, String filterCondition, String[] filtervalues) {
		try {
			XMLObject object = service.getBPList(projectnumber, bpname, fieldnames, filterCondition, filtervalues);
			// logger.info(object.getXmlcontents());

			return MapUtils.map2Java(clazz, ServiceUtil.readPUXML(XMLHeaderConstants.BP, object));
		} catch (RemoteException | DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @Title: getBPDetail 
	 * @Description: TODO
	 * @param @param clazz
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param fieldnames
	 * @param @param filterCondition
	 * @param @param filtervalues
	 * @param @return 
	 * @return Object
	 * @throws
	 */
	public Object getBPDetail(Class clazz, String projectnumber, String bpname, String[] fieldnames, String filterCondition, String[] filtervalues) {
		try {
			XMLObject object = service.getBPList(projectnumber, bpname, fieldnames, filterCondition, filtervalues);
			// logger.info(object.getXmlcontents());

			List<Object> list = MapUtils.map2Java(clazz, ServiceUtil.readPUXML(XMLHeaderConstants.BP, object));
			if (list != null) {
				return list.get(0);
			} else {
				return null;
			}
		} catch (RemoteException | DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @Title: getBPDetailWithItem 
	 * @Description: TODO
	 * @param @param clazz
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param fieldnames
	 * @param @param filterCondition
	 * @param @param filtervalues
	 * @param @return 
	 * @return Object
	 * @throws
	 */
	public Object getBPDetailWithItem(Class clazz, String projectnumber, String bpname, String[] fieldnames, String filterCondition, String[] filtervalues) {
		try {
			XMLObject object = service.getBPList(projectnumber, bpname, fieldnames, filterCondition, filtervalues);
			// logger.info(object.getXmlcontents());

			List<Object> list = MapUtils.map2Java(clazz, ServiceUtil.readPUChildXML(XMLHeaderConstants.BP, object));
			if (list != null) {
				return list.get(0);
			} else {
				return null;
			}
		} catch (RemoteException | DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @Title: getBPDetailWithItemXML 
	 * @Description: TODO
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param fieldnames
	 * @param @param filterCondition
	 * @param @param filtervalues
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public String getBPDetailWithItemXML(String projectnumber, String bpname, String[] fieldnames, String filterCondition, String[] filtervalues) {
		try {
			return service.getBPList(projectnumber, bpname, fieldnames, filterCondition, filtervalues).getXmlcontents();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @Title: addBPRecordGeneral 
	 * @Description: TODO
	 * @param @param clazz
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param object
	 * @param @return 
	 * @return String
	 * @throws   Add  方法的调用来源使用XML写入的
	 */
	public String addBPRecordGeneral(Class clazz, String projectnumber, String bpname, Object object) {
		try {
			String xxx = ServiceUtil.createXML(false, XMLHeaderConstants.BP, clazz, MapUtils.java2Map(object));

			return processResult(service.createBPRecord(projectnumber, bpname, xxx));
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "RemoteException";
		}
	}

	/**
	 * @Title: addBPRecordGeneral 
	 * @Description: TODO
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param xml
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public String addBPRecordGeneral(String projectnumber, String bpname, String xml) {
		try {
			return processResult(service.createBPRecord(projectnumber, bpname, xml));
		} catch (RemoteException e) {
			e.printStackTrace();
			return "RemoteException";
		}
	}

	/**
	 * update upper but not lineitem
	 * @Title: updBPRecordGeneral 
	 * @Description: TODO
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param xml
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public String updBPRecordGeneral(String projectnumber, String bpname, String xml) {
		try {
			logger.info(xml);
			return processResult(service.updateBPRecord(projectnumber, bpname, xml));
		} catch (RemoteException e) {
			e.printStackTrace();
			return "RemoteException";
		}
	}

	/**
	 * update upper and also lineitem
	 * @Title: updBPRecordGeneralV2 
	 * @Description: TODO
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param xml
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public String updBPRecordGeneralV2(String projectnumber, String bpname, String xml) {
		try {
			return processResult(service.updateBPRecordV2(projectnumber, bpname, xml, null));
		} catch (RemoteException e) {
			e.printStackTrace();
			return "RemoteException";
		}
	}

	/**
	 * @Title: addBPRecordListGeneral 
	 * @Description: TODO
	 * @param @param clazz
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param object
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public String addBPRecordListGeneral(Class clazz, String projectnumber, String bpname, Object object) {
		// TODO Auto-generated method stub
		try {
			String xml = ServiceUtil.createXMLFromList(false, XMLHeaderConstants.BP, clazz, MapUtils.java2MapList(object));
			logger.info("object xml: " + xml);

			// return processResult(service.createBPRecord(projectnumber, bpname, xml));
			return processResult(service.createBPRecord(projectnumber, bpname, xml));
		} catch (RemoteException e) {
			e.printStackTrace();
			return "RemoteException";
		}
	}

	/**
	 * @Title: addBPRecordMeeting 
	 * @Description: TODO
	 * @param @param projectnumber
	 * @param @param bpname
	 * @param @param map
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public String addBPRecordMeeting(String projectnumber, String bpname, Map<String, String> map) {
		// TODO Auto-generated method stub
		try {
			return processResult(service.createBPRecord(projectnumber, bpname, ServiceUtil.createXMLInCondition(true, XMLHeaderConstants.BP, map)));
			// status: 200 means success ， otherwise failed ...
		} catch (RemoteException e) {
			e.printStackTrace();
			return "RemoteException";
		}
	}

	/**
	 * @Title: processResult 
	 * @Description: TODO
	 * @param @param result
	 * @param @return 
	 * @return String
	 * @throws
	 */
	private String processResult(XMLObject result) {
		// status: 200 means success ， otherwise failed ...
		if (result.getStatusCode() == 200) {
			return "Success";
		} else {
			String ress = "Failed, ";
			for (String string : result.getErrorStatus()) {
				ress += string;
			}
			return ress;
		}
	}
}